package pars.itqiqi.handler.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import pars.itqiqi.handler.pojo.User;

import java.io.IOException;
import java.util.Objects;

/**
 * @author kevin_琪琪
 * @version 1.0
 * @description
 * @date 2022-07-13 16:13
 */
@Slf4j
@RestController
public class TestController {

    /**
     * 解析 json 数据
     *
     */
    @PostMapping("/login1")
    public String login1(@RequestBody User user) {
        log.info("login1()方法的username:{}", user.getUsername());
        log.info("login1()方法的password:{}", user.getPassword());
        return user.toString();
    }

    /**
     * 解析 multipart/form-data 数据
     *
     */
    @PostMapping("/login2")
    public String login2(User user) {
        log.info("login2()方法的username:{}", user.getUsername());
        log.info("login2()方法的password:{}", user.getPassword());
        return user.toString();
    }

    /**
     * 文件上传
     *
     */
    @PostMapping("/upload")
    public String upload(@RequestParam MultipartFile file) {
        log.info("文件的名称为:{}", file.getOriginalFilename());
        log.info("文件的大小为:{}", String.format("%.2f",(file.getSize()/1024.1/1024.0)) + "MB");
        log.info("文件的类型为:{}", file.getContentType());
        log.info("文件的扩展名为:{}", Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".")));
        return null;
    }

}
